library user_data_model;

import 'package:flutter/cupertino.dart';
import 'package:package_model/package_model.dart';
import 'package:user_data_model/data/user_data.dart';
import 'package:rx_shared_preferences/rx_shared_preferences.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:convert' as convert;
export 'package:user_data_model/data/user_data.dart';
export 'user_bloc.dart';

class UserDataPlugin {
  static Future saveUserData(UserData userData) async {
    RxSharedPreferences sp = await _getSharedPreference();
    sp.setString("userData", convert.jsonEncode(userData.toJson()));
    return;
  }

  static Future<UserData> getUserDataFromLocal() async {
    RxSharedPreferences sp = await _getSharedPreference();
    String userJson = await sp.getString("userData") ?? "";
    print("user from share ${userJson.toString()}");
    if (userJson.isEmpty) {
      return null;
    } else {
      return UserData.fromJson(convert.jsonDecode(userJson));
    }
  }

  static Future<String> getRefreshToken() async {
    RxSharedPreferences sp = await _getSharedPreference();
    String refreshToken = await sp.getString("refreshToken") ?? "";
    return refreshToken;
  }

  static initRefreshToken(String refreshToken) async {
    RxSharedPreferences sp = await _getSharedPreference();
    sp.setString("refreshToken", refreshToken);
  }

  static Future<RxSharedPreferences> _getSharedPreference() async {

    return RxSharedPreferences(
      SharedPreferences.getInstance(),
      const MyLogger(),
    );
  }
}
class MyLogger extends LoggerAdapter {
  const MyLogger();
}
